// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: OOZ Setup Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 40 : TYPE_OOZSETUP

// Function declarations
reserve function OOZSetup_Function97
reserve function OOZSetup_Function98
reserve function OOZSetup_Function99
reserve function OOZSetup_Function100
reserve function OOZSetup_Function101
reserve function OOZSetup_Function102

// Static Values

// Tables
public table OOZSetup_table38
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
end table

public table OOZSetup_table39
	816, 12, 817, 6, 818, 10, 819, 4
end table

public table OOZSetup_table40
	820, 7, 821, 7, 822, 7, 823, 7
end table

public table OOZSetup_table41
	824, 7, 826, 7, 828, 7, 830, 7
end table

//0xF884
private table OOZSetup_table0
	0xC00000, 0xE06000, 0xE0C000, 0xE0E080
end table

function OOZSetup_Function97
	object.animationSpeed = object.speed
	if object.animationSpeed < 0
		FlipSign(object.animationSpeed)
	end if
	object.animationSpeed *= 120
	object.animationSpeed /= 0x60000
	object.animationSpeed += 20
end function


function OOZSetup_Function98
	if object.animation != ANI_SKIDDING
		temp7 = 1
	else
		temp7 = 0
	end if
	CallFunction(PlayerObject_HandleMovement)
	if object.gravity == GRAVITY_AIR
		object.state = PlayerObject_HandleAir
		CallFunction(PlayerObject_HandleAirMovement)
	else
		CallFunction(PlayerObject_ResetOnFloor)
		if object.speed == 0
			if object.collisionMode == CMODE_FLOOR
				if object.value1 < 240
					object.animation = ANI_STOPPED
					object.value1++
				else
					object.animation = ANI_WAITING
					if stage.playerListPos == 2
						object.value1++
						if object.value1 == 834
							object.value1 = 0
							object.animation = ANI_STOPPED
						end if
					end if
				end if
			end if
		else
			object.value1 = 0
			if object.speed > 0
				if object.speed < 0x5F5C2
					object.animation = ANI_WALKING
					CallFunction(PlayerObject_WalkAnimSpd)
				else
					if object.speed > 0x9FFFF
						object.animation = ANI_PEELOUT
					else
						object.animation = ANI_RUNNING
					end if
					CallFunction(PlayerObject_RunAnimSpd)
				end if
			else
				if object.speed > -0x5F5C2
					object.animation = ANI_WALKING
					CallFunction(PlayerObject_WalkAnimSpd)
				else
					if object.speed < -0x9FFFF
						object.animation = ANI_PEELOUT
					else
						object.animation = ANI_RUNNING
					end if
					CallFunction(PlayerObject_RunAnimSpd)
				end if
			end if
		end if
		if object.value14 > 0
			if temp7 == 1
				PlaySfx(SfxName[Skidding], 0)
			end if
			object.animation = ANI_SKIDDING
			object.animationSpeed = 0
			object.value14--
			if ringTimer == 0
				CreateTempObject(TypeName[Dust Puff], 0, object.xpos, object.ypos)
				object[tempObjectPos].iypos += object.collisionBottom
				object[tempObjectPos].drawOrder = object.value18
			end if
			if object.speed > 0
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
		end if
		if object.collisionMode == CMODE_FLOOR
			if object.pushing == 2
				object.animation = ANI_PUSHING
				object.animationSpeed = 0
			end if
		end if
		if object.jumpPress == 1
			CallFunction(PlayerObject_StartJump)
		end if
	end if
end function


function OOZSetup_Function99
	if object.controlLock > 0
		object.controlLock--
		Sin256(temp0, object.angle)
		temp0 *= 0x2000
		temp0 >>= 8
		object.speed += temp0
	else
		if object.left == 1
			temp0 = object.value20
			FlipSign(temp0)
			if object.speed > temp0
				object.speed -= 0x400
			end if
			if object.speed <= 0
				object.direction = FLIP_X
			end if
		end if
		if object.right == 1
			if object.speed < object.value20
				object.speed += 0x400
			end if
			if object.speed >= 0
				object.direction = FLIP_NONE
			end if
		end if
		temp0 = object.left
		temp0 |= object.right
		if temp0 == 0
			if object.speed > 0
				object.speed -= 4
				if object.speed < 0
					object.speed = 0
				end if
			else
				object.speed += 4
				if object.speed > 0
					object.speed = 0
				end if
			end if
			if object.speed > 0x2000
				Sin256(temp0, object.angle)
				temp0 *= 0x2000
				temp0 >>= 8
				object.speed += temp0
			end if
			if object.speed < -0x2000
				Sin256(temp0, object.angle)
				temp0 *= 0x2000
				temp0 >>= 8
				object.speed += temp0
			end if
			if object.angle > 192
				if object.angle < 228
					if object.speed > -0x10000
						if object.speed < 0x10000
							object.controlLock = 30
						end if
					end if
				end if
			end if
			if object.angle > 28
				if object.angle < 64
					if object.speed > -0x10000
						if object.speed < 0x10000
							object.controlLock = 30
						end if
					end if
				end if
			end if
		else
			Sin256(temp0, object.angle)
			temp0 *= 0x2000
			temp0 >>= 8
			object.speed += temp0
			if object.right == 1
				if object.left == 0
					if object.angle > 192
						if object.angle < 228
							if object.speed < 0x28000
								if object.speed > -0x20000
									object.controlLock = 30
								end if
							end if
						end if
					end if
				end if
			else
				if object.left == 1
					if object.angle > 28
						if object.angle < 64
							if object.speed > -0x28000
								if object.speed < 0x20000
									object.controlLock = 30
								end if
							end if
						end if
					end if
				end if
			end if
		end if
	end if
end function


function OOZSetup_Function100
	CallFunction(OOZSetup_Function99)
	if object.gravity == GRAVITY_AIR
		object.state = PlayerObject_HandleAir
		CallFunction(PlayerObject_HandleAirMovement)
	else
		CallFunction(PlayerObject_ResetOnFloor)
		if object.speed == 0
			if object.collisionMode == CMODE_FLOOR
				if object.value1 < 240
					object.animation = ANI_STOPPED
					object.value1++
				else
					object.animation = ANI_WAITING
					if stage.playerListPos == 2
						object.value1++
						if object.value1 == 834
							object.value1 = 0
							object.animation = ANI_STOPPED
						end if
					end if
				end if
			end if
		else
			object.value1 = 0
			if object.animation != ANI_WATERSLIDE
				if object.speed > 0
					if object.speed < 0x30000
						object.animation = ANI_WALKING
						CallFunction(OOZSetup_Function97)
					else
						object.animation = ANI_RUNNING
						object.animationSpeed = 240
					end if
				else
					if object.speed > -0x30000
						object.animation = ANI_WALKING
						CallFunction(OOZSetup_Function97)
					else
						object.animation = ANI_RUNNING
						object.animationSpeed = 240
					end if
				end if
			end if
		end if
		object.value14 = 0
		if object.collisionMode == CMODE_FLOOR
			if object.pushing == 2
				object.animation = ANI_PUSHING
				object.animationSpeed = 0
			end if
		end if
		if object.jumpPress == 1
			CallFunction(PlayerObject_StartJump)
		end if
	end if
end function


function OOZSetup_Function101
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("OilOcean_F.ogg", 0, 0x396E4, 0x1F40)
			break
		case 2
			SetMusicTrack("OilOcean_F.ogg", 0, 0x396E4)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("OilOcean_F.ogg", 0, 0x396E4)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function OOZSetup_Function102
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("OilOcean.ogg", 0, 0x47AC4, 0x30D4)
			break
		case 2
			SetMusicTrack("OilOcean.ogg", 0, 0x47AC4)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("OilOcean.ogg", 0, 0x47AC4)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


event ObjectMain
	if options.vsMode == 0
		if stage.actNum == 2
			if object.value7 == 0
				if screen.cameraY < 256
					if screen.cameraX > 0x2140
						temp0 = screen.yoffset
						temp0 *= 44
						temp0 <<= 8
						temp1 = screen.yoffset
						temp1 >>= 2
						temp1 += 128
						tileLayer[1].scrollPos = screen.yoffset
						tileLayer[1].scrollPos *= temp1
						tileLayer[1].scrollPos <<= 8
						tileLayer[1].scrollPos -= temp0
						FlipSign(tileLayer[1].scrollPos)
						tileLayer[1].parallaxFactor = temp1
						object.value7 = 1
					end if
				end if
			end if
		end if
	end if
	object.value0++
	if object.value0 > 7
		object.value0 = 0
		tileLayer[1].deformationOffset++
		RotatePalette(0, 170, 173, 0)
	end if
	if object.value1 < 2
		GetTableValue(temp0, object.value2, OOZSetup_table39)
		Copy16x16Tile(756, temp0)
		object.value2++
		GetTableValue(object.value1, object.value2, OOZSetup_table39)
		object.value2++
		object.value2 &= 7
	else
		object.value1--
	end if
	if object.value3 < 2
		GetTableValue(temp0, object.value4, OOZSetup_table40)
		Copy16x16Tile(757, temp0)
		GetTableValue(temp0, object.value4, OOZSetup_table41)
		Copy16x16Tile(758, temp0)
		temp0++
		Copy16x16Tile(759, temp0)
		object.value4++
		GetTableValue(object.value3, object.value4, OOZSetup_table40)
		object.value4++
		object.value4 &= 7
	else
		object.value3--
	end if
	if object.value5 < 2
		temp0 = object.value6
		temp0 += 768
		Copy16x16Tile(760, temp0)
		temp0++
		Copy16x16Tile(761, temp0)
		temp0++
		Copy16x16Tile(762, temp0)
		temp0++
		Copy16x16Tile(763, temp0)
		temp0 += 21
		Copy16x16Tile(764, temp0)
		temp0++
		Copy16x16Tile(765, temp0)
		temp0++
		Copy16x16Tile(766, temp0)
		temp0++
		Copy16x16Tile(767, temp0)
		object.value5 = 18
		object.value6 += 4
		object.value6 %= 24
	else
		object.value5--
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object[currentPlayer].state == PlayerObject_Blank
			if object[currentPlayer].ypos < -0x800000
				object[currentPlayer].state = PlayerObject_HandleAir
				object[currentPlayer].gravity = GRAVITY_AIR
				object[currentPlayer].value35 = 0
				object[currentPlayer].tileCollisions = 1
			end if
		end if
		if object[currentPlayer].animation == ANI_TWIRL
			object[currentPlayer].frame %= 24
		end if
		temp1 = object[currentPlayer].xpos
		temp1 >>= 16
		temp2 = object[currentPlayer].ypos
		temp2 >>= 16
		temp2 += 6
		Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_ANGLEB)
		switch temp0
		case 1
			if object[currentPlayer].yvel > -1
				object[currentPlayer].state = OOZSetup_Function98
				if object[currentPlayer].animation == ANI_JUMPING
					object[currentPlayer].animation = ANI_WALKING
				end if
				object[currentPlayer].gravity = GRAVITY_GROUND
				object[currentPlayer].yvel = 0
				object[currentPlayer].ypos += 0x10000
			end if
			break
		case 3
			object[currentPlayer].value43 = 0
			if object[currentPlayer].gravity == GRAVITY_GROUND
				object[currentPlayer].state = OOZSetup_Function100
			end if
			break
		case 5
			object[currentPlayer].value43 = 0
			if object[currentPlayer].gravity == GRAVITY_GROUND
				object[currentPlayer].state = OOZSetup_Function100
				object[currentPlayer].animation = ANI_WATERSLIDE
				Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_DIRECTION)
				switch temp0
				case 0
				case 2
					if object[currentPlayer].speed > -0x80000
						object[currentPlayer].speed -= 0x4000
					end if
					break
				case 1
				case 3
					if object[currentPlayer].speed < 0x80000
						object[currentPlayer].speed += 0x4000
					end if
				end switch
				if object[currentPlayer].speed < 0
					object[currentPlayer].direction = FLIP_X
				else
					object[currentPlayer].direction = FLIP_NONE
				end if
				if currentPlayer == 0
					if object.value8 == 0
						PlaySfx(SfxName[Oil Slide], 0)
					end if
					object.value8++
					object.value8 &= 15
				end if
			end if
			break
		end switch
	next
end event


event ObjectStartup
	SetMusicTrack("OilOcean.ogg", 0, 0x47AC4)
	SpeedUpMusic = OOZSetup_Function101
	SlowDownMusic = OOZSetup_Function102
	animalType1 = TypeName[Rocky]
	animalType2 = TypeName[Pecky]
	SetPaletteEntry(0, 192, 0)
	object[10].type = TypeName[OOZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	stage.newYBoundary2 = 0x768
	stage.deathBoundary = stage.newYBoundary2
	stage.deathBoundary -= 8
	stage.deathBoundary <<= 16
	arrayPos0 = 0
	while arrayPos0 < 576
		temp0 = arrayPos0
		temp0 &= 63
		GetTableValue(stage[arrayPos0].deformationData2, temp0, OOZSetup_table38)
		arrayPos0++
	loop
	
	switch stage.actNum
		case 0
		case 1
			//LoadPalette("OOZAct1Pal.act", 0, 128, 128, 256)
			break
		case 2
			foreach (TypeName[Sign Post], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].xpos = 11073
				object[arrayPos0].xpos <<= 16
				object[arrayPos0].ypos = 579
				object[arrayPos0].ypos <<= 16
			next
			tileLayer[1].scrollPos = 0
			tileLayer[1].parallaxFactor = 44
			if screen.cameraY < 640
				if screen.cameraX > 0x20FC
					tileLayer[1].parallaxFactor = 128
					object[10].value7 = 1
				end if
			end if
			LoadPalette("OOZAct2Pal.act", 0, 128, 128, 256)
			break
	end switch
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
